package jp.ne.hatena.d.digitalsoul.warikan.roles

import jp.ne.hatena.d.digitalsoul.warikan.math._
import jp.ne.hatena.d.digitalsoul.warikan.models._
import jp.ne.hatena.d.digitalsoul.warikan.repositories._

trait PieMaker {
	
	var participants:Map[String, Participant]
	
	def createPie(slopeId:String):Map[String, Fraction] = {
		val amountPie = createAmountPie(slopeId)
		val totalWeight = sumWeightUp(amountPie)
		createPercentagePie(amountPie, totalWeight)
	}
	
	
	private def createAmountPie(slopeId:String):Map[String, Int] = {
		var pie = Map[String, Int]()
		for (participant <- participants.values) {
			val weight = SlopeRepository.weightOf(slopeId, participant.role)
			pie += (participant.name -> weight)
		}
		pie
	}

	private def sumWeightUp(amountPie:Map[String, Int]):Int =
		(0 /: amountPie.values) ((sum, weight) => sum + weight)
		
	private def createPercentagePie(amountPie:Map[String, Int], 
			totalWeight:Int):Map[String, Fraction] = {	
		var percentagePie = Map[String, Fraction]()
		for (it <- amountPie.elements) {
			percentagePie += it._1 -> new Fraction(it._2, totalWeight)
		}
		percentagePie
	}

}